<?php
if(!defined('IN_IBEIKE')) {
	exit('Access Denied');
}

class table_user_credit extends common_table
{
	public function __construct() {

		$this->_table = 'user_credit';
		$this->_pk    = 'uid';

		parent::__construct();
	}
	public function fetch_all_by_uid($uid){
		if(!empty($uid)&&$uid){
			return DB::fetch_all('SELECT * FROM %t WHERE uid=%s',array($this->_table,$uid));//返回数组包含各个字段的值
		}
		return false;
	}
	public function update_intfield_chage_by_uid($uid,$fielddata,$order){//$fielddata是键为字段,值为修改的数目,$order为1增加0减
		if(!empty($uid) && !empty($fielddata) && !empty($order)){
			$sign="";
			foreach ($fielddata as $k => $v){
				$datakey=$sign.$k;
				$sign=",";
			}
			$old_num=self::get_intfield_num_by_uid($uid,$datakey);//查询该主键的数字字段值,返回数组
			if($old_num!=false){
				foreach ($fielddata as $k => $v){
					if($order==1){
						$old_num[$k]=$old_num[$k]+$v;
					}else{
						$old_num[$k]=$old_num[$k]-$v;
					}
				}
			}else{
				return false;
			}
			$condition=$this->_pk."='".$uid."'";
			return DB::update($this->_table,$old_num,$condition);//更新数据
		}else{
			return false;
		}
	}
	public static function get_intfield_num_by_uid($uid,$datakey){//按主键查询数字类的字段的值为多少
		if(!empty($uid) && !empty($datakey)){
			return DB::fetch_first('SELECT %s FROM %t WHERE uid=%s',array($datakey,$this->_table,$uid));
		}else{
			return false;
		}
	}

	
}
?>